package org.batatasframework.support.searchable.result;

import java.sql.ResultSet;
import java.util.List;

import org.batatasframework.support.database.sql.field.SelectFragment;
import org.batatasframework.support.database.values.FieldValue;
import org.batatasframework.support.searchable.ResultHeadInfo;
import org.batatasframework.support.searchable.SearchableType;


public class StatisticsResultBuffer extends AbstractGroupResultStream<StatisticsResultHead, StatisticsResultRow>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2511973867785066306L;
	
	public StatisticsResultBuffer(SelectFragment group) throws Exception {
		super(group);
	}

	@Override
	protected StatisticsResultHead buildHead(SelectFragment sf) {
		if(groupSelectFragment == sf) return null;
		return new StatisticsResultHead(sf);
	}

	@Override
	protected StatisticsResultRow buildGroupRow(List<StatisticsResultHead> heads, FieldValue value) {
		return new StatisticsResultRow(heads, value.targetValue());
	}
	
	@Override
	public SearchableType getSearchableType() {
		return SearchableType.STATIS;
	}
	
//	@Override
//	protected void internalRead(ResultSet rs) throws Exception {
//		super.internalRead(rs);
//		
//		heads.add(0, new SearchResultHead(groupSelectFragment));
//	}

	@Override
	protected void beforeFlush(List<ResultHeadInfo> one) {
		one.add(new SearchResultHead(groupSelectFragment));
		this.rows.add(statsRow);
		super.beforeFlush(one);
	}
	
	private StatisticsResultRow statsRow;
	
	@Override
	protected void internalRead(ResultSet rs) throws Exception {
		statsRow = new StatisticsResultRow(heads, "合计");
		super.internalRead(rs);
	}
	
	@Override
	protected void read(ResultSet rs, StatisticsResultRow groupRow) throws Exception {
		super.read(rs, groupRow);
		statsRow.read(rs);
	}
}
